package com.ml.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ml.feign.SettingRemote;
import com.ml.model.Symbol;
import com.ml.service.ExSymbolService;
import com.ml.util.DataMapper;
import com.virgil.redis.util.RedisUtil;

@RestController
public class SettingController implements SettingRemote {
	@Autowired
	RedisUtil redisUtil;
	@Autowired
	ExSymbolService ExSymbolService;

	@Override
	public List<Symbol> getSymbols() {
		String redisString = redisUtil.get("Sys:SymbolInfo");
		List<Symbol> symbols = null;
		if (StringUtils.isNotEmpty(redisString)) {
			symbols = JSON.parseArray(redisString, Symbol.class);
		} else {
			// Cache from DB
			symbols = DataMapper.INSTANCE.exSymbolsToSymbols(ExSymbolService.list());
			redisUtil.set("Sys:SymbolInfo", JSON.toJSONString(symbols));
		}
		return symbols;
	}

}
